#!/usr/bin/env python3
"""
项目结构初始化脚本
创建完整的项目目录结构和基础文件
"""

import os
from pathlib import Path

def create_directories():
    """创建项目目录结构"""
    print("📁 创建项目目录结构...")
    
    directories = [
        # 源代码目录
        "src",
        "src/camp_battle",
        "src/camp_battle/core",
        "src/camp_battle/units",
        "src/camp_battle/ai",
        "src/camp_battle/map",
        "src/camp_battle/ui",
        "src/camp_battle/utils",
        "src/camp_battle/config",
        
        # 资源目录
        "assets",
        "assets/images",
        "assets/sounds",
        "assets/fonts",
        
        # 配置目录
        "config",
        
        # 数据目录
        "data",
        "data/saves",
        "data/replays",
        
        # 测试目录
        "tests",
        "tests/unit",
        "tests/integration",
        
        # 日志目录
        "logs",
        
        # 文档目录
        "docs/images",
    ]
    
    for directory in directories:
        Path(directory).mkdir(parents=True, exist_ok=True)
        print(f"  ✅ {directory}")
    
    print("✅ 目录结构创建完成")

def create_init_files():
    """创建__init__.py文件"""
    print("\n📝 创建__init__.py文件...")
    
    init_files = [
        "src/__init__.py",
        "src/camp_battle/__init__.py",
        "src/camp_battle/core/__init__.py",
        "src/camp_battle/units/__init__.py",
        "src/camp_battle/ai/__init__.py",
        "src/camp_battle/map/__init__.py",
        "src/camp_battle/ui/__init__.py",
        "src/camp_battle/utils/__init__.py",
        "src/camp_battle/config/__init__.py",
        "tests/__init__.py",
        "tests/unit/__init__.py",
        "tests/integration/__init__.py",
    ]
    
    for init_file in init_files:
        Path(init_file).touch()
        print(f"  ✅ {init_file}")
    
    print("✅ __init__.py文件创建完成")

def create_main_entry():
    """创建主入口文件"""
    print("\n🚀 创建主入口文件...")
    
    main_content = '''#!/usr/bin/env python3
"""
CampBattleView2D 主入口文件
"""

import sys
import os
from pathlib import Path

# 添加src目录到Python路径
current_dir = Path(__file__).parent
src_dir = current_dir / "src"
sys.path.insert(0, str(src_dir))

def main():
    """主函数"""
    try:
        from camp_battle.core.game import Game
        
        print("🎮 启动 CampBattleView2D")
        game = Game()
        game.run()
        
    except ImportError as e:
        print(f"❌ 导入错误: {e}")
        print("请确保项目结构正确且已安装所有依赖")
        sys.exit(1)
    except Exception as e:
        print(f"❌ 运行错误: {e}")
        import traceback
        traceback.print_exc()
        sys.exit(1)

if __name__ == "__main__":
    main()
'''
    
    with open("main.py", "w", encoding="utf-8") as f:
        f.write(main_content)
    
    print("  ✅ main.py")
    print("✅ 主入口文件创建完成")

def create_config_files():
    """创建配置文件"""
    print("\n⚙️ 创建配置文件...")
    
    # 游戏配置文件
    game_config = '''[game]
title = CampBattleView2D
window_width = 1200
window_height = 800
fps = 60
fullscreen = false

[debug]
show_fps = true
show_vision_boundaries = true
show_unit_info = true
log_level = INFO

[gameplay]
game_duration = 600
initial_spawn_delay = 60
position_spawn_interval = 90
minion_spawn_interval = 60

[ai]
decision_interval = 1.0
strategic_interval = 30.0
tactical_interval = 5.0
'''
    
    with open("config/game.ini", "w", encoding="utf-8") as f:
        f.write(game_config)
    
    # 单位配置文件
    units_config = '''# 单位属性配置文件
# 基础模板定义

[hero_template]
hp = 1500
atk = 120
ap = 50
def = 40
mr = 35
armor = 0
as = 1.8
crit = 12
crit_dmg = 180
acc = 92
dodge = 8
block = 15
block_val = 12
spd = 3
range = 2
sight = 6
stealth = 0
detect = 3
mp = 200
mp_regen = 3
hp_regen = 2
shield = 0

[tank_template]
hp = 1000
atk = 80
ap = 0
def = 50
mr = 30
armor = 0
as = 2.0
crit = 5
crit_dmg = 150
acc = 90
dodge = 5
block = 20
block_val = 15
spd = 2
range = 1
sight = 5
stealth = 0
detect = 2
mp = 0
mp_regen = 0
hp_regen = 1
shield = 0

[melee_template]
hp = 600
atk = 150
ap = 0
def = 30
mr = 20
armor = 0
as = 1.2
crit = 15
crit_dmg = 200
acc = 95
dodge = 10
block = 5
block_val = 10
spd = 3.5
range = 1
sight = 4
stealth = 0
detect = 1
mp = 0
mp_regen = 0
hp_regen = 0
shield = 0

[ranged_template]
hp = 400
atk = 180
ap = 0
def = 15
mr = 25
armor = 0
as = 2.0
crit = 20
crit_dmg = 180
acc = 85
dodge = 15
block = 0
block_val = 0
spd = 2.5
range = 6
sight = 7
stealth = 0
detect = 3
mp = 0
mp_regen = 0
hp_regen = 0
shield = 0

[support_template]
hp = 500
atk = 60
ap = 100
def = 25
mr = 40
armor = 0
as = 2.5
crit = 8
crit_dmg = 140
acc = 80
dodge = 12
block = 10
block_val = 8
spd = 2.8
range = 3
sight = 6
stealth = 0
detect = 4
mp = 150
mp_regen = 5
hp_regen = 0
shield = 0
'''
    
    with open("config/units.ini", "w", encoding="utf-8") as f:
        f.write(units_config)
    
    print("  ✅ config/game.ini")
    print("  ✅ config/units.ini")
    print("✅ 配置文件创建完成")

def create_gitignore():
    """创建.gitignore文件"""
    print("\n📋 创建.gitignore文件...")
    
    gitignore_content = '''# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Virtual environments
venv/
env/
ENV/
env.bak/
venv.bak/

# IDEs
.vscode/
.idea/
*.swp
*.swo
*~

# Game specific
logs/
*.log
data/saves/
data/replays/
screenshots/
profile.stats

# OS generated files
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db
'''
    
    with open(".gitignore", "w", encoding="utf-8") as f:
        f.write(gitignore_content)
    
    print("  ✅ .gitignore")
    print("✅ .gitignore文件创建完成")

def create_readme():
    """创建README文件"""
    print("\n📖 创建README文件...")
    
    readme_content = '''# CampBattleView2D

2D平面自动战斗观赏游戏

## 项目描述

四个AI控制的阵营在2D地图上进行自动战斗，玩家作为观众观看整个战斗过程。每个阵营有独特的特色和战术风格，通过AI决策进行资源收集、单位生产、战术配合等操作。

## 游戏特色

- 🎮 **纯观赏性**：无需玩家操作，专注观看AI博弈
- 🏛️ **四大阵营**：钢铁军团、自然守护者、暗影刺客、元素法师
- 🎯 **战术深度**：英雄、岗位角色、小兵的多层次战斗系统
- 👁️ **视野系统**：真实的战争迷雾和侦察机制
- 🎨 **像素艺术**：经典的像素风格视觉效果

## 快速开始

### 环境要求

- Python 3.10+
- 已安装requirements.txt中的依赖

### 运行游戏

```bash
python main.py
```

### 项目结构

```
├── src/camp_battle/     # 主要代码
│   ├── core/           # 核心引擎
│   ├── units/          # 单位系统
│   ├── ai/             # AI系统
│   ├── map/            # 地图系统
│   └── ui/             # 用户界面
├── assets/             # 游戏资源
├── config/             # 配置文件
├── tests/              # 测试代码
└── docs/               # 文档
```

## 开发状态

🚧 项目正在开发中...

## 许可证

MIT License
'''
    
    with open("README.md", "w", encoding="utf-8") as f:
        f.write(readme_content)
    
    print("  ✅ README.md")
    print("✅ README文件创建完成")

def main():
    """主函数"""
    print("🚀 CampBattleView2D 项目初始化")
    print("=" * 50)
    
    # 创建目录结构
    create_directories()
    
    # 创建__init__.py文件
    create_init_files()
    
    # 创建主入口文件
    create_main_entry()
    
    # 创建配置文件
    create_config_files()
    
    # 创建.gitignore
    create_gitignore()
    
    # 创建README
    create_readme()
    
    print("\n🎉 项目初始化完成！")
    print("\n📋 后续步骤:")
    print("1. 运行游戏: python main.py")
    print("2. 查看项目结构: tree . (或 ls -la)")
    print("3. 开始开发核心代码")
    
    print("\n📁 项目结构已创建:")
    print("├── src/camp_battle/     # 主要代码目录")
    print("├── assets/              # 游戏资源目录")
    print("├── config/              # 配置文件目录")
    print("├── tests/               # 测试代码目录")
    print("├── main.py              # 主入口文件")
    print("└── README.md            # 项目说明")

if __name__ == "__main__":
    main()
